//
//  PTStuLifeServiceReleaseViewController.m
//  PlanTech
//  发布页面
//  Created by ruifeng song on 2018/7/10.
//  Copyright © 2018年  androidlongs. All rights reserved.
//

#import "PTStuLifeServiceReleaseViewController.h"
#import "PTStuLifeServiceReleaseModel.h"
#import "SINACustomModelPickView.h"
#import "SINASchoolModel.h"
#import "PTReceiveTimePickView.h"
#import "PTCoreDefine.h"
#import "PTCoreManager+LifeService.h"
#import "SINACalendarUtils.h"
#import "PTCoreObject.h"
#import "SINACalendarUtils.h"

@interface PTStuLifeServiceReleaseViewController ()<UITableViewDelegate,UITableViewDataSource,CustomModelPickViewDelegate,PTReceiveTimePickViewDelegate,UITextFieldDelegate,UITextViewDelegate,UIAlertViewDelegate>
{
    CGFloat bottomHeight ;
}
@property(nonatomic,strong)UITableView * releaseTableView;
@property (nonatomic, strong) NSArray * releaseByMealArray;
@property(nonatomic,copy)NSString *receiveTimeStr;
@property(nonatomic,copy)NSString *startTime;
@property(nonatomic,copy)NSString *entTime;

//备注
@property(nonatomic,copy)NSString * remark;
//收货地址
@property(nonatomic,copy)NSString *userAddress;
//服务内容12
@property (nonatomic,copy)NSString *serviceContent1;
@property (nonatomic,copy)NSString *serviceContent2;

//酬金数据
@property(nonatomic,strong)NSMutableArray * moneyArray;

//服务类型数据
@property(nonatomic,strong)NSMutableArray *serviceTypeArray;

//服务类型
@property(nonatomic,strong) SINASchoolModel *serviceTypeModel;

//酬金
@property(nonatomic,strong) SINASchoolModel *moneySelectModel;

@property(nonatomic,assign) NSInteger kbHeight;
//@property(nonatomic,assign) NSInteger kbDuration;

@end

@implementation PTStuLifeServiceReleaseViewController

#pragma mark - 键盘通知
- (void)addNoticeForKeyboard {
    
    //注册键盘出现的通知
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification object:nil];
    //注册键盘消失的通知
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification object:nil];
}

///键盘显示事件
- (void) keyboardWillShow:(NSNotification *)notification {
    //获取键盘高度，在不同设备上，以及中英文下是不同的
    //    CGFloat kbHeight = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
    
    self.kbHeight = 100;
    
    //    // 取得键盘的动画时间，这样可以在视图上移的时候更连贯
    //    double duration = [[notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    //
    //    self.kbDuration = duration;
    //
}

///键盘消失事件
- (void) keyboardWillHide:(NSNotification *)notify {
    // 键盘动画时间
    double duration = [[notify.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    
    //视图下沉恢复原状
    [UIView animateWithDuration:duration animations:^{
        self.releaseTableView.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight- TOP_HEIGHT);
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"发布";
    self.view.backgroundColor = BaseViewBackgroundColor;
    
    [self addNoticeForKeyboard];
    
    self.startTime = @"";
    self.entTime = @"";
    
    [self createView];
    
    if (self.listModel) {
        //从我的发布页跳转过来(重新发布操作)
        [self resetReleaseData];
    }
    else
    {
        [self requestData];
    }
}

-(void)resetReleaseData
{
    //酬金
    SINASchoolModel *rewardModel = [[SINASchoolModel alloc]init];
    rewardModel.schoolId=[NSString stringWithFormat:@"%@",self.listModel.reward];
    rewardModel.schoolName=[NSString stringWithFormat:@"%@RMB",self.listModel.reward];
    self.moneySelectModel = rewardModel;
    
    self.remark = self.listModel.remark;
    self.userAddress = self.listModel.userAddress;
    
    //服务类型
    SINASchoolModel *schoolModel = [[SINASchoolModel alloc]init];
    schoolModel.schoolId = [NSString stringWithFormat:@"%@",self.listModel.type];
    switch ([self.listModel.type integerValue]) {
        case PTLifeService_ExpressType:
            schoolModel.schoolName =@"代取快递";
            self.serviceContent1 = self.listModel.destinationAddress;
            self.serviceContent2 = self.listModel.accessCode;
            break;
        case PTLifeService_PrintType:
            schoolModel.schoolName =@"打印复印";
            self.serviceContent1= [NSString stringWithFormat:@"%@",self.listModel.copies];
            self.serviceContent2= self.listModel.destinationAddress;
            break;
        case PTLifeService_BuyMealType:
            schoolModel.schoolName =@"捎饭带饭";
            self.serviceContent1 = self.listModel.destinationName;
            self.serviceContent2 = self.listModel.destinationAddress;
            break;
        case PTLifeService_HotboomType:
            schoolModel.schoolName =@"超市代购";
            self.serviceContent1 = self.listModel.destinationName;
            self.serviceContent2 = self.listModel.destinationAddress;
            break;
    }
    
    self.serviceTypeModel = schoolModel;
    
    [self.releaseTableView reloadData];
}

-(void)createView
{
    [self.view addSubview:self.releaseTableView];
}

-(void)requestData
{
    for (int i=0; i<=20; i++) {
        
        SINACommonPopModel *model=[[SINACommonPopModel alloc]init];
        SINASchoolModel *schoolModel = [[SINASchoolModel alloc]init];
        schoolModel.schoolId=[NSString stringWithFormat:@"%d",i];
        schoolModel.schoolName=[NSString stringWithFormat:@"%dRMB",i];
        model.title=schoolModel.schoolName;
        model.model = schoolModel;
        
        [self.moneyArray addObject:model];
    }
    
    NSArray * types = [NSArray arrayWithObjects:@"代取快递",@"打印复印",@"捎饭带饭",@"超市代购", nil];
    for (int i = 0; i<4; i++) {
        
        SINACommonPopModel *model=[[SINACommonPopModel alloc]init];
        SINASchoolModel *schoolModel = [[SINASchoolModel alloc]init];
        schoolModel.schoolId=[NSString stringWithFormat:@"%d",(i+8)];
        schoolModel.schoolName=[NSString stringWithFormat:@"%@",[types objectAtIndex:i]];
        model.title=schoolModel.schoolName;
        model.model = schoolModel;
        
        [self.serviceTypeArray addObject:model];
    }
    
    [self.releaseTableView reloadRow:0 inSection:0 withRowAnimation:UITableViewRowAnimationAutomatic];
}

-(NSMutableArray *)serviceTypeArray
{
    if (_serviceTypeArray == nil) {
        
        _serviceTypeArray = [NSMutableArray array];
    }
    return _serviceTypeArray;
}

-(NSMutableArray *)moneyArray
{
    if (_moneyArray ==nil) {
        _moneyArray =[NSMutableArray array];
    }
    return _moneyArray;
}

- (NSArray *)releaseByMealArray {
    
    if(_releaseByMealArray == nil) {
        
        NSArray *dataArr =[NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"PTStuReleaseBuyMeal" ofType:@"plist"]];
        
        NSMutableArray *mArray = [NSMutableArray array];
        
        for (NSArray * arr in dataArr) {
            
            [mArray addObject:arr];
        }
        _releaseByMealArray = mArray;
        
    }
    
    return _releaseByMealArray;
    
}

-(UITableView *)releaseTableView
{
    if (_releaseTableView == nil) {
        
        _releaseTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight- TOP_HEIGHT) style:UITableViewStylePlain];
        _releaseTableView.delegate = self;
        _releaseTableView.dataSource = self;
        _releaseTableView.backgroundColor = [UIColor clearColor];
    }
    return _releaseTableView;
}


#pragma mark - UITableViewDelegate & UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 5;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (section) {
        case 0:
            return 1;
            break;
        case 1:
            return 4;
            break;
        case 2:
            return 1;
            break;
        case 3:
            return 2;
            break;
        case 4:
            return 1;
            break;
    }
    return 0;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [[UIView alloc]init];
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section ==4) {
        if (DEVICE_IS_iPhoneX) {
            
            bottomHeight = self.releaseTableView.height - self.releaseTableView.contentSize.height - BOTTOM_HEIGHT;
            
            return bottomHeight;
        }
        return 80;
    }
    return 0;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (section == 4) {
        UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 80)];
        
        UIButton * submitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [submitBtn setTitle:@"提交" forState:UIControlStateNormal];
        submitBtn.layer.cornerRadius = 20;
        submitBtn.layer.masksToBounds = YES;
        submitBtn.backgroundColor = PTColor_349ef7;
        [submitBtn setFrame:CGRectMake(15, 20, (ScreenWidth - 30), 40)];
        [submitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [submitBtn addTarget:self action:@selector(onClickSubmitButton:) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:submitBtn];
        
        if (DEVICE_IS_iPhoneX) {
            
            [view setFrame:CGRectMake(0, 0, ScreenWidth, bottomHeight)];
            [submitBtn setFrame:CGRectMake(15, (bottomHeight - 40), (ScreenWidth - 30), 40)];
        }
        
        return view;
    }
    return [[UIView alloc]init];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 15;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 4) {
        
        return 90;
    }
    return 45;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ReleaseCellIdentifier = @"ReleaseCell";
    
    UITableViewCell * releaseCell = [tableView dequeueReusableCellWithIdentifier:ReleaseCellIdentifier];
    if (releaseCell == nil) {
        
        releaseCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ReleaseCellIdentifier];
        releaseCell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        if (indexPath.section == 4) {
            
            UILabel * remarkLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 15, 35, 14)];
            remarkLabel.font = PTFONT_15;
            remarkLabel.textColor = PTColor_2e2e2e;
            remarkLabel.tag = 111;
            [releaseCell.contentView addSubview:remarkLabel];
            
            UITextView * remarkTextView = [[UITextView alloc]initWithFrame:CGRectMake(remarkLabel.right  +10, 5,(ScreenWidth - remarkLabel.right - 10 -15),80)];
            remarkTextView.tag = 4444;
            remarkTextView.textColor = PTColor_2e2e2e;
            remarkTextView.font = PTFONT_15;
            remarkTextView.delegate = self;
            remarkTextView.returnKeyType = UIReturnKeyDone;
            [releaseCell.contentView addSubview:remarkTextView];
        }
        else if (indexPath.section ==1 && indexPath.row ==3)
        {
            releaseCell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
            releaseCell.textLabel.font = PTFONT_15;
            releaseCell.textLabel.textColor = PTColor_2e2e2e;
            
            UITextField * tf = [[UITextField alloc]initWithFrame:CGRectMake(100, 2.5, ScreenWidth - 100 - 35, 40)];
            tf.placeholder = @"请填写";
            tf.font = PTFONT_15;
            tf.tag = 1111;
            tf.delegate = self;
            tf.textColor =PTColor_878787;
            tf.returnKeyType = UIReturnKeyDone;
            tf.textAlignment = NSTextAlignmentRight;
            [releaseCell.contentView addSubview:tf];
        }
        else if (indexPath.section == 3)
        {
            releaseCell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
            releaseCell.textLabel.font = PTFONT_15;
            releaseCell.textLabel.textColor = PTColor_2e2e2e;
            
            UITextField * tf = [[UITextField alloc]initWithFrame:CGRectMake(100, 2.5, ScreenWidth - 100 - 35, 40)];
            tf.placeholder = @"请填写";
            tf.font = PTFONT_15;
            tf.delegate = self;
            tf.tag = 1111 *(indexPath.row + 2);
            tf.textColor =PTColor_878787;
            tf.returnKeyType = UIReturnKeyDone;
            tf.textAlignment = NSTextAlignmentRight;
            [releaseCell.contentView addSubview:tf];
        }
        else
        {
            releaseCell.textLabel.font = PTFONT_15;
            releaseCell.detailTextLabel.font = PTFONT_15;
            releaseCell.textLabel.textColor = PTColor_2e2e2e;
            releaseCell.detailTextLabel.textColor = PTColor_878787;
            if ((indexPath.section ==1 && indexPath.row ==0) ||(indexPath.section ==1 && indexPath.row ==1)) {
                releaseCell.accessoryType=UITableViewCellAccessoryNone;
            }
            else
            {
                releaseCell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
            }
           
        }
    }
    
    NSArray * arr =[self.releaseByMealArray objectAtIndex:indexPath.section];
    NSDictionary * dict = [arr objectAtIndex:indexPath.row];
    
    if(indexPath.section ==0)
    {
        releaseCell.textLabel.text = [dict objectForKey:@"titleName"];
        if (self.serviceTypeModel == nil) {
            
            releaseCell.detailTextLabel.text = [dict objectForKey:@"contentValue"];
        }
        else
        {
            releaseCell.detailTextLabel.text = self.serviceTypeModel.schoolName;
        }
    }
    else if (indexPath.section ==1)
    {
        releaseCell.textLabel.text = [dict objectForKey:@"titleName"];
        
        SINAUserModel *userModel = [[SINACommonUserHelper share]getUserModel];
        
        if (indexPath.row ==0) {
            //收件人姓名
            if (self.listModel) {
                
                releaseCell.detailTextLabel.text = self.listModel.userName;
            }
            else
            {
                releaseCell.detailTextLabel.text =userModel.userRealName;
            }
        }
        else if (indexPath.row ==1)
        {
            //收件人电话
            if (self.listModel) {
                
                releaseCell.detailTextLabel.text = self.listModel.userPhone;
            }
            else
            {
                releaseCell.detailTextLabel.text = userModel.userPhone;
            }
        }
        else if (indexPath.row ==2) {
            
            //收货时间
            if (self.receiveTimeStr && self.receiveTimeStr.length >0) {
                
                releaseCell.detailTextLabel.text = self.receiveTimeStr;
            }
            else
            {
                releaseCell.detailTextLabel.text = [dict objectForKey:@"contentValue"];
            }
        }
        else if (indexPath.row == 3)
        {
            UITextField * tf = [releaseCell.contentView viewWithTag:1111];
            tf.text = self.userAddress;
        }
        else
        {
            releaseCell.detailTextLabel.text = [dict objectForKey:@"contentValue"];
        }
    }
    else if(indexPath.section == 2)
    {
        releaseCell.textLabel.text = [dict objectForKey:@"titleName"];
        if (self.moneySelectModel ==nil) {
            releaseCell.detailTextLabel.text = [dict objectForKey:@"contentValue"];
        }
        else
        {
            releaseCell.detailTextLabel.text = self.moneySelectModel.schoolName;
        }
    }
    else if (indexPath.section ==3)
    {
        //判断当前服务类型，显示对应文案
        if (self.serviceTypeModel == nil) {
            
            //默认显示
            if (indexPath.row == 0) {
                
                releaseCell.textLabel.text = @"取货码";
            }
            else
            {
                releaseCell.textLabel.text = @"快递点";
            }
        }
        else
        {
            NSString * str1 = @"";
            NSString * str2 = @"";
            switch ([self.serviceTypeModel.schoolId integerValue]) {
                case PTLifeService_ExpressType: //代取快递
                    str1 = @"取货码";
                    str2 = @"快递点";
                    break;
                case PTLifeService_PrintType: //打印复印
                    str1 = @"打印份数";
                    str2 = @"打印地点";
                    break;
                case PTLifeService_BuyMealType: //捎饭带饭
                    str1 = @"餐厅名称";
                    str2 = @"餐厅地址";
                    break;
                case PTLifeService_HotboomType: //超市代购
                    str1 = @"超市名称";
                    str2 = @"超市地址";
                    break;
            }
            
            if (indexPath.row == 0) {
                
                releaseCell.textLabel.text = str1;
                
                UITextField * tf = [releaseCell.contentView viewWithTag:2222];
                tf.text = self.serviceContent1;
            }
            else
            {
                releaseCell.textLabel.text = str2;
                
                UITextField * tf = [releaseCell.contentView viewWithTag:3333];
                tf.text = self.serviceContent2;
            }
        }
    }
    else if (indexPath.section ==4) {
        
        UILabel * lbl = [releaseCell.contentView viewWithTag:111];
        lbl.text = [dict objectForKey:@"titleName"];
        
        UITextView * tv = [releaseCell.contentView viewWithTag:4444];
        tv.text = self.remark;
    }
    else
    {
        releaseCell.textLabel.text = [dict objectForKey:@"titleName"];
        releaseCell.detailTextLabel.text = [dict objectForKey:@"contentValue"];
    }
    return releaseCell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section ==0) {
        [self serviceTypeSelectFunction];
    }
    else if(indexPath.section ==1)
    {
        if (indexPath.row ==2) {
            
            [self selectServiceTime];
        }
    }
    else if (indexPath.section == 2) {
        
        [self moneySelectFunction];
    }
}

-(void)serviceTypeSelectFunction
{
    [self askLeaveSelectTypeFunction:self.serviceTypeArray andWithTitle:@"服务类型" andTag:102];
}

-(void)moneySelectFunction{
    [self askLeaveSelectTypeFunction:self.moneyArray andWithTitle:@"酬金" andTag:101];
}
-(void) askLeaveSelectTypeFunction:(NSMutableArray*) arrayData andWithTitle:(NSString *)title andTag:(NSInteger)tag{
    
    SINACustomModelPickView *pickerSingle = [[SINACustomModelPickView alloc]init];
    
    [pickerSingle setDataArray:arrayData];
    [pickerSingle setDefalutSelectRowStr:0];
    pickerSingle.titleLabel.text=title;
    pickerSingle.tag=tag;
    
    [pickerSingle setDelegate:self];
    
    [pickerSingle show];
    [self.view endEditing:YES];
}

#pragma mark - CustomModelPickViewDelegate Method
- (void)customModelPickView:(SINACustomModelPickView *)customPickView selectedTitle:(SINACommonPopModel *)selectedModel
{
    if(customPickView.tag==102){
        self.serviceTypeModel=(SINASchoolModel*)selectedModel.model;
        
        [self.releaseTableView reloadRow:0 inSection:0 withRowAnimation:UITableViewRowAnimationAutomatic];
        [self.releaseTableView reloadSection:3 withRowAnimation:UITableViewRowAnimationAutomatic];
        
    }else if(customPickView.tag==101){
        self.moneySelectModel=(SINASchoolModel*)selectedModel.model;
        
        [self.releaseTableView reloadRow:0 inSection:2 withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}

- (void)dismissCustomPickView:(SINACustomModelPickView *)customPickView
{
    
}

-(void)selectServiceTime{
    [self.view endEditing:YES];
    PTReceiveTimePickView *pickerDate = [[PTReceiveTimePickView alloc]init];
    //标题
    pickerDate.titleLabel.text=@"收货时间";
    
    //设置默认选中开始时间（当前小时加1）
    [pickerDate setDefalutSelectRowStr:[NSString stringWithFormat:@"%d", [SINACalendarUtils getHour] +1]];
    
    //选择回调代理
    [pickerDate setDelegate:self];
    //显示
    [pickerDate show];
}

#pragma mark - PTReceiveTimePickViewDelegate Method

- (void)serviceTimeSelectPickView:(PTReceiveTimePickView *)customPickView day:(NSString *)day startHour:(NSString *)startHour endHour:(NSString *)endHour
{
    NSDateFormatter * df = [[NSDateFormatter alloc] init];
    df.dateFormat = @"yyyy年MM月dd日 HH:mm:ss";
    
    NSString *currentTime =  [[SINACalendarUtils alloc]getCurrentTime:@"yyyy年"];
    NSDate * date1 = [SINACalendarUtils getNowDateFromatAnDate:[df dateFromString:[NSString stringWithFormat:@"%@%@ %@:00:00",currentTime,day,startHour]]];
    
    self.startTime = [NSString stringWithFormat:@"%@",[SINACalendarUtils timeStampFromUTCDate:date1]];
   
    if ([[SINACalendarUtils currentTimeStr] doubleValue] > [self.startTime doubleValue]) {
        
        WISAlertView * alert = [[WISAlertView alloc]initWithTitle:@"提示" message:@"开始时间必须大于当前时间" sureBtn:@"确定" cancleBtn:nil];
        [alert showXLAlertView];
        
        return;
    }
    
    //条件判断（结束时间必须大于开始时间）
    if ([endHour integerValue] <= [startHour integerValue]) {
        
        WISAlertView * alert = [[WISAlertView alloc]initWithTitle:@"提示" message:@"结束时间必须大于开始时间" sureBtn:@"确定" cancleBtn:nil];
        [alert showXLAlertView];
        
        return;
    }
     NSDate * date2= [SINACalendarUtils getNowDateFromatAnDate:[df dateFromString:[NSString stringWithFormat:@"%@%@ %@:00:00",currentTime,day,endHour]]];
    
    self.entTime = [NSString stringWithFormat:@"%@",[SINACalendarUtils timeStampFromUTCDate:date2]];
    
     self.receiveTimeStr =[NSString stringWithFormat:@"%@ %@:00-%@:00",day,startHour,endHour];

    [self.releaseTableView reloadRow:2 inSection:1 withRowAnimation:UITableViewRowAnimationAutomatic];
}

- (void)dismissServiceTimeSelectPickView:(PTReceiveTimePickView *)customPickView
{
    
}

#pragma mark - 点击提交发布按钮
-(void)onClickSubmitButton:(id)sender
{
    
    if (self.serviceTypeModel == nil) {
        
        WISAlertView * alert = [[WISAlertView alloc]initWithTitle:@"提示" message:@"请选择服务类型" sureBtn:@"确定" cancleBtn:nil];
        [alert showXLAlertView];
        
        return;
    }
    
    UITableViewCell * remarkCell = [self.releaseTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:4]];
    UITextView * remarkTV = [remarkCell.contentView viewWithTag:4444];
    
    UITableViewCell * serviceCell1 = [self.releaseTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:3]];
    
    UITableViewCell * serviceCell2 = [self.releaseTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:3]];
    
    UITextField *tf1 = [serviceCell1.contentView viewWithTag:2222];
    UITextField * tf2 = [serviceCell2.contentView viewWithTag:3333];
    
    UITableViewCell * addressCell =[self.releaseTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:3 inSection:1]];
    UITextField * addressTF = [addressCell.contentView viewWithTag:1111];
    
    NSString * destinationAddress = tf2.text;
    NSString * destinationName = tf1.text;
    
    NSString *copies = tf1.text;
    NSString *accessCode = tf1.text;
    
    if (self.startTime.length ==0 || self.entTime.length ==0) {
        
        WISAlertView * alert = [[WISAlertView alloc]initWithTitle:@"提示" message:@"请选择收货时间" sureBtn:@"确定" cancleBtn:nil];
        [alert showXLAlertView];
        
        return;
    }
    
    if (addressTF.text.length ==0) {
        
        WISAlertView * alert = [[WISAlertView alloc]initWithTitle:@"提示" message:@"请输入收货地址" sureBtn:@"确定" cancleBtn:nil];
        [alert showXLAlertView];
        
        return;
    }
    
    if (self.moneySelectModel == nil) {
        
        WISAlertView * alert = [[WISAlertView alloc]initWithTitle:@"提示" message:@"请选择酬金" sureBtn:@"确定" cancleBtn:nil];
        [alert showXLAlertView];
        
        return;
    }
    
    SINAUserModel *userModel = [[SINACommonUserHelper share]getUserModel];
    
    //时间转换
    NSString * serviceUUId = @"";
    if (self.listModel) {
        //重新发布需求
        serviceUUId = self.listModel.serviceUUID;
    }
    
    WeakSelf;
    if ([self.serviceTypeModel.schoolId integerValue]== PTLifeService_ExpressType) {
        
        if (accessCode.length == 0) {
            
            WISAlertView * alert = [[WISAlertView alloc]initWithTitle:@"提示" message:@"请输入取货码" sureBtn:@"确定" cancleBtn:nil];
            [alert showXLAlertView];
            
            return;
        }
        
        if (destinationAddress.length == 0) {
            
            WISAlertView * alert = [[WISAlertView alloc]initWithTitle:@"提示" message:@"请输入快递点" sureBtn:@"确定" cancleBtn:nil];
            [alert showXLAlertView];
            
            return;
        }
        
        //代取快递
        [[PTCoreManager shareInstance]performPostReleaseOrderWithType:[self.serviceTypeModel.schoolId integerValue] expressCat:@"" purchaseCat:@"" userUUID:userModel.userUid startTime:self.startTime endTime:self.entTime userAddress:addressTF.text reward:[self.moneySelectModel.schoolId integerValue] destinationName:@"" destinationAddress:destinationAddress accessCode:accessCode copies:@"" remark:remarkTV.text universityId:userModel.universityid userName:userModel.userName userPhone:userModel.userPhone serviceUUID:serviceUUId Completion:^(PTMJsonResult * retObject, NSError * _Nullable retError) {
            
            if (retObject) {
                
                if ([retObject.code integerValue]== kPTCore_Response_Code) {
                    
                    UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"发布成功" delegate:weakSelf cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                    alertView.tag = 1111;
                    [alertView show];
                }
            }
            else
            {
                
                WISAlertView * alertView = [[WISAlertView alloc]initWithTitle:@"提示" message:@"提交错误" sureBtn:@"确定" cancleBtn:nil];
                [alertView showXLAlertView];
                
            }
            
        }];
    }
    else if ([self.serviceTypeModel.schoolId integerValue]== PTLifeService_PrintType)
    {
        
        if (copies.length == 0) {
            
            WISAlertView * alert = [[WISAlertView alloc]initWithTitle:@"提示" message:@"请输入打印份数" sureBtn:@"确定" cancleBtn:nil];
            [alert showXLAlertView];
            
            return;
        }
        
        if (destinationAddress.length == 0) {
            
            WISAlertView * alert = [[WISAlertView alloc]initWithTitle:@"提示" message:@"请输入打印地点" sureBtn:@"确定" cancleBtn:nil];
            [alert showXLAlertView];
            
            return;
        }
        
        //打印复印
        [[PTCoreManager shareInstance]performPostReleaseOrderWithType:[self.serviceTypeModel.schoolId integerValue] expressCat:@"" purchaseCat:@"" userUUID:userModel.userUid startTime:self.startTime endTime:self.entTime userAddress:addressTF.text reward:[self.moneySelectModel.schoolId integerValue] destinationName:@"" destinationAddress:destinationAddress accessCode:@"" copies:copies remark:remarkTV.text universityId:userModel.universityid userName:userModel.userName userPhone:userModel.userPhone serviceUUID:serviceUUId Completion:^(PTMJsonResult * retObject, NSError * _Nullable retError) {
            
            if (retObject) {
                
                if ([retObject.code integerValue]== kPTCore_Response_Code) {
                    
                    UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"发布成功" delegate:weakSelf cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                    alertView.tag = 1111;
                    [alertView show];
                }
            }
            else
            {
                
                WISAlertView * alertView = [[WISAlertView alloc]initWithTitle:@"提示" message:@"提交错误" sureBtn:@"确定" cancleBtn:nil];
                [alertView showXLAlertView];
                
                
            }
            
        }];
    }
    else
    {
        if ([self.serviceTypeModel.schoolId integerValue]== PTLifeService_BuyMealType) {
            
            if (destinationName.length == 0) {
                
                WISAlertView * alert = [[WISAlertView alloc]initWithTitle:@"提示" message:@"请输入餐厅名称" sureBtn:@"确定" cancleBtn:nil];
                [alert showXLAlertView];
                
                return;
            }
            
            if (destinationAddress.length == 0) {
                
                WISAlertView * alert = [[WISAlertView alloc]initWithTitle:@"提示" message:@"请输入餐厅地址" sureBtn:@"确定" cancleBtn:nil];
                [alert showXLAlertView];
                
                return;
            }
        }
        else
        {
            if (destinationName.length == 0) {
                
                WISAlertView * alert = [[WISAlertView alloc]initWithTitle:@"提示" message:@"请输入超市名称" sureBtn:@"确定" cancleBtn:nil];
                [alert showXLAlertView];
                
                return;
            }
            
            if (destinationAddress.length == 0) {
                
                WISAlertView * alert = [[WISAlertView alloc]initWithTitle:@"提示" message:@"请输入超市地址" sureBtn:@"确定" cancleBtn:nil];
                [alert showXLAlertView];
                
                return;
            }
        }
        
        //捎饭带饭，超市代购
        [[PTCoreManager shareInstance]performPostReleaseOrderWithType:[self.serviceTypeModel.schoolId integerValue] expressCat:@"" purchaseCat:@"" userUUID:userModel.userUid startTime:self.startTime endTime:self.entTime userAddress:addressTF.text reward:[self.moneySelectModel.schoolId integerValue] destinationName:destinationName destinationAddress:destinationAddress accessCode:@"" copies:@"" remark:remarkTV.text universityId:userModel.universityid userName:userModel.userName userPhone:userModel.userPhone serviceUUID:serviceUUId Completion:^(PTMJsonResult * retObject, NSError * _Nullable retError) {
            
            if (retObject) {
                if ([retObject.code integerValue]== kPTCore_Response_Code) {
                    
                    UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"发布成功" delegate:weakSelf cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                    alertView.tag = 1111;
                    [alertView show];
            
                }
            }
            else
            {
                WISAlertView * alertView = [[WISAlertView alloc]initWithTitle:@"提示" message:@"提交错误" sureBtn:@"确定" cancleBtn:nil];
                [alertView showXLAlertView];
            }
            
        }];
    }
}

-(void)clearCacheData
{
    self.remark = @"";
    self.userAddress = @"";
    self.serviceContent1 = @"";
    self.serviceContent2 = @"";
    self.startTime = @"";
    self.entTime = @"";
    self.receiveTimeStr = @"";
    self.moneySelectModel = nil;
    [self.releaseTableView reloadRow:0 inSection:0 withRowAnimation:UITableViewRowAnimationAutomatic];
    [self.releaseTableView reloadData];
}

#pragma mark - UITextFieldDelegate Method

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [UIView animateWithDuration:0.2 animations:^{
        self.releaseTableView.frame = CGRectMake(0, -self.kbHeight, ScreenWidth, ScreenHeight- TOP_HEIGHT);
    }];
}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if ([string isEqualToString:@"\n"]) {
        [textField resignFirstResponder];
        return NO;
    }
    return YES;
}


#pragma mark - UITextViewDelegate Method
-(void)textViewDidBeginEditing:(UITextView *)textView
{
    [UIView animateWithDuration:0.2 animations:^{
        self.releaseTableView.frame = CGRectMake(0, -150, ScreenWidth, ScreenHeight- TOP_HEIGHT);
    }];
}


- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}

#pragma mark UIAlertViewDelegate Method
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag ==1111 && buttonIndex ==0) {
        
        [self clearCacheData];

        [[NSNotificationCenter defaultCenter]postNotificationName:kPT_Global_Notification_RefreshLifeServiceList object:nil];
      
        [[NSNotificationCenter defaultCenter]postNotificationName:kPT_Global_Notification_RefreshMyReleaseList object:nil];
        
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
